package com.glueball.crm.module.client.ui;

import com.glueball.crm.module.client.model.Company;
import com.glueball.crm.module.client.model.EmailAddress;
import com.glueball.crm.module.client.model.PhoneNumber;
import com.glueball.crm.module.common.ui.CrmWidget;
import com.glueball.crm.module.common.ui.EditableWidget.StartView;
import com.glueball.crm.module.common.ui.EmailAddressWidget;
import com.glueball.crm.module.common.ui.KeyValueWidget;
import com.glueball.crm.module.common.ui.PhoneNumberWidget;
import com.glueball.crm.module.util.KeyValue;
import com.glueball.crm.module.util.URLHelper;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class CompanyWidget extends CrmWidget {

	@UiField SimplePanel     namePanel;
	@UiField HorizontalPanel nameCtrlPanel;
	@UiField SimplePanel     shortNamePanel;
	@UiField HorizontalPanel shortNameCtrlPanel;
	@UiField SimplePanel     registrationNumberPanel;
	@UiField HorizontalPanel registrationNumberCtrlPanel;
	@UiField SimplePanel     taxNumberPanel;
	@UiField HorizontalPanel taxNumberCtrlPanel;
	@UiField SimplePanel     VATIdPanel;
	@UiField HorizontalPanel VATIdCtrlPanel;
	@UiField SimplePanel     emailAddressPanel;
	@UiField HorizontalPanel emailAddressCtrlPanel;
	@UiField SimplePanel     phoneNumberPanel;
	@UiField HorizontalPanel phoneNumberCtrlPanel;
	
	private final String clientId;
	private final Company entity;
	
	private KeyValueWidget nameWidget;
	private KeyValueWidget shortNameWidget;
	private KeyValueWidget registrationNumberWidget;
	private KeyValueWidget taxNumberWidget;
	private KeyValueWidget VATIdWidget;
	private PhoneNumberWidget phoneNumberWidget;
	private EmailAddressWidget emailAddressWidget;
	
	private static CompanyWidgetUiBinder uiBinder = GWT.create(CompanyWidgetUiBinder.class);

	interface CompanyWidgetUiBinder extends UiBinder<Widget, CompanyWidget> {
	}

	public CompanyWidget(final Company entity, final Composite parent, final String clientId) {
		this.entity = entity;
		this.clientId = clientId;
		this.widget = uiBinder.createAndBindUi(this);
		refresh();
	}

	public void refresh() {

		nameWidget = new KeyValueWidget(
				new KeyValue("name", entity.getName()),
				namePanel,
				nameCtrlPanel,
				StartView.SHOW,
				URLHelper.MODIFY_CLIENT_DATA + clientId);

		shortNameWidget = new KeyValueWidget(
				new KeyValue("shortName", entity.getShortName()),
				shortNamePanel,
				shortNameCtrlPanel,
				StartView.SHOW,
				URLHelper.MODIFY_CLIENT_DATA + clientId);

		registrationNumberWidget = new KeyValueWidget(
				new KeyValue("registrationNumber", entity.getRegistrationNumber()),
				registrationNumberPanel,
				registrationNumberCtrlPanel,
				StartView.SHOW,
				URLHelper.MODIFY_CLIENT_DATA + clientId);
		
		taxNumberWidget = new KeyValueWidget(
				new KeyValue("taxNumber", entity.getTaxNumber()),
				taxNumberPanel,
				taxNumberCtrlPanel,
				StartView.SHOW,
				URLHelper.MODIFY_CLIENT_DATA + clientId);

		VATIdWidget = new KeyValueWidget(
				new KeyValue("vatId", entity.getVatId()),
				VATIdPanel,
				VATIdCtrlPanel,
				StartView.SHOW,
				URLHelper.MODIFY_CLIENT_DATA + clientId);

		phoneNumberWidget = new PhoneNumberWidget(
				entity.getPhone() == null ? new PhoneNumber() : entity.getPhone(),
				phoneNumberPanel,
				phoneNumberCtrlPanel,
				StartView.SHOW,
				URLHelper.SAVE_CLIENT_COMPANY_PHONENUMBER + clientId);

		emailAddressWidget = new EmailAddressWidget(
				entity.getEmail() == null ? new EmailAddress() : entity.getEmail(),
				emailAddressPanel,
				emailAddressCtrlPanel,
				StartView.SHOW,
				URLHelper.SAVE_CLIENT_COMPANY_EMAILADDRESS + clientId);
	}

	public KeyValueWidget getNameWidget() {
		return nameWidget;
	}

	public void setNameWidget(KeyValueWidget nameWidget) {
		this.nameWidget = nameWidget;
	}

	public KeyValueWidget getShortNameWidget() {
		return shortNameWidget;
	}

	public void setShortNameWidget(KeyValueWidget shortNameWidget) {
		this.shortNameWidget = shortNameWidget;
	}

	public KeyValueWidget getRegistrationNumberWidget() {
		return registrationNumberWidget;
	}

	public void setRegistrationNumberWidget(KeyValueWidget registrationNumberWidget) {
		this.registrationNumberWidget = registrationNumberWidget;
	}

	public KeyValueWidget getTaxNumberWidget() {
		return taxNumberWidget;
	}

	public void setTaxNumberWidget(KeyValueWidget taxNumberWidget) {
		this.taxNumberWidget = taxNumberWidget;
	}

	public KeyValueWidget getVATIdWidget() {
		return VATIdWidget;
	}

	public void setVATIdWidget(KeyValueWidget vATIdWidget) {
		VATIdWidget = vATIdWidget;
	}

	public PhoneNumberWidget getPhoneNumberWidget() {
		return phoneNumberWidget;
	}

	public void setPhoneNumberWidget(PhoneNumberWidget phoneNumberWidget) {
		this.phoneNumberWidget = phoneNumberWidget;
	}

	public EmailAddressWidget getEmailAddressWidget() {
		return emailAddressWidget;
	}

	public void setEmailAddressWidget(EmailAddressWidget emailAddressWidget) {
		this.emailAddressWidget = emailAddressWidget;
	}
}